package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.utils.ObjectSerializableUtil;
import com.ssy.lingxi.logistics.api.dto.request.*;
import com.ssy.lingxi.logistics.entity.ReceiverAddress;
import com.ssy.lingxi.logistics.entity.ShipperAddress;
import com.ssy.lingxi.logistics.model.enums.CommonBooleanEnum;
import com.ssy.lingxi.logistics.model.enums.ReceiverAddressCreateTypeEnum;
import com.ssy.lingxi.logistics.repository.ReceiverAddressRepository;
import com.ssy.lingxi.logistics.repository.ShipperAddressRepository;
import com.ssy.lingxi.logistics.service.IMessageQueueHandleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 内部feign接口 - 收发货地址相关接口实现
 * @author wyx
 * @version 2.0.0
 * @date 2022-02-16
 **/
@Service
public class MessageQueueHandleServiceImpl implements IMessageQueueHandleService {
    private final static Logger logger = LoggerFactory.getLogger(MessageQueueHandleServiceImpl.class);

    @Resource
    private ShipperAddressRepository shipperAddressRepository;
    @Resource
    private ReceiverAddressRepository receiverAddressRepository;


    /**
     * 同步门店收发货地址数据到物流服务
     * @param jsonMessage 消息json字符串对象
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void syncLogisticsAddress(String jsonMessage) {
        LogisticsAddressSyncQueueVO queueVO = ObjectSerializableUtil.deserialize(jsonMessage, LogisticsAddressSyncQueueVO.class);
        if (queueVO == null) {
            logger.error("新增或修改物流地址消息反序列化错误 =>" + jsonMessage);
            return;
        }

        //物流发货地址
        LogisticsDeliverAddress logisticsDeliverAddress = queueVO.getLogisticsDeliverAddress();

        if (logisticsDeliverAddress == null) {
            shipperAddressRepository.deleteByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
        } else {
            ShipperAddress shipperAddress = shipperAddressRepository.findByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
            if (shipperAddress == null) {
                shipperAddress = new ShipperAddress();
            }
            shipperAddress.setShipperName(logisticsDeliverAddress.getShipperName());
            shipperAddress.setProvinceCode(logisticsDeliverAddress.getProvinceCode());
            shipperAddress.setProvinceName(logisticsDeliverAddress.getProvinceName());
            shipperAddress.setCityCode(logisticsDeliverAddress.getCityCode());
            shipperAddress.setCityName(logisticsDeliverAddress.getCityName());
            shipperAddress.setDistrictCode(logisticsDeliverAddress.getDistrictCode());
            shipperAddress.setDistrictName(logisticsDeliverAddress.getDistrictName());
            shipperAddress.setStreetCode(logisticsDeliverAddress.getStreetCode());
            shipperAddress.setStreetName(logisticsDeliverAddress.getStreetName());
            shipperAddress.setAddress(logisticsDeliverAddress.getAddress());
            shipperAddress.setPostalCode(logisticsDeliverAddress.getPostalCode());
            shipperAddress.setAreaCode(logisticsDeliverAddress.getAreaCode());
            shipperAddress.setPhone(logisticsDeliverAddress.getPhone());
            shipperAddress.setTel(logisticsDeliverAddress.getTel());
            shipperAddress.setIsDefault(logisticsDeliverAddress.getIsDefault());
            shipperAddress.setMemberId(queueVO.getMemberId());
            shipperAddress.setRoleId(queueVO.getMemberRoleId());
            shipperAddress.setCreateTime(System.currentTimeMillis());
            shipperAddress.setStoreId(queueVO.getStoreId());
            shipperAddress.setStoreStatus(queueVO.getStoreStatus());
            shipperAddress.setIsStore(CommonBooleanEnum.YES.getCode());
            shipperAddress.setStoreName(queueVO.getStoreName());
            shipperAddressRepository.saveAndFlush(shipperAddress);
        }

        //物流收货地址
        LogisticsReceiveAddress logisticsReceiveAddress = queueVO.getLogisticsReceiveAddress();

        if (logisticsReceiveAddress == null) {
            receiverAddressRepository.deleteByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
        } else {
            ReceiverAddress receiverAddress = receiverAddressRepository.findByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
            if (receiverAddress == null) {
                receiverAddress = new ReceiverAddress();
            }
            receiverAddress.setReceiverName(logisticsReceiveAddress.getReceiverName());
            receiverAddress.setProvinceCode(logisticsReceiveAddress.getProvinceCode());
            receiverAddress.setProvinceName(logisticsReceiveAddress.getProvinceName());
            receiverAddress.setCityCode(logisticsReceiveAddress.getCityCode());
            receiverAddress.setCityName(logisticsReceiveAddress.getCityName());
            receiverAddress.setDistrictCode(logisticsReceiveAddress.getDistrictCode());
            receiverAddress.setDistrictName(logisticsReceiveAddress.getDistrictName());
            receiverAddress.setStreetCode(logisticsReceiveAddress.getStreetCode());
            receiverAddress.setStreetName(logisticsReceiveAddress.getStreetName());
            receiverAddress.setAddress(logisticsReceiveAddress.getAddress());
            receiverAddress.setAreaCode(logisticsReceiveAddress.getAreaCode());
            receiverAddress.setPostalCode(logisticsReceiveAddress.getPostalCode());
            receiverAddress.setPhone(logisticsReceiveAddress.getPhone());
            receiverAddress.setTel(logisticsReceiveAddress.getTel());
            receiverAddress.setIsDefault(logisticsReceiveAddress.getIsDefault());
            receiverAddress.setMemberId(queueVO.getMemberId());
            receiverAddress.setRoleId(queueVO.getMemberRoleId());
            receiverAddress.setCreateTime(System.currentTimeMillis());
            receiverAddress.setCreateType(ReceiverAddressCreateTypeEnum.LOGISTICS.getCode());
            receiverAddress.setStoreId(queueVO.getStoreId());
            receiverAddress.setStoreStatus(queueVO.getStoreStatus());
            receiverAddress.setIsStore(CommonBooleanEnum.YES.getCode());
            receiverAddress.setStoreName(queueVO.getStoreName());
            receiverAddressRepository.saveAndFlush(receiverAddress);
        }
    }

    /**
     * 删除门店同步删除物流服务地址数据
     * @param jsonMessage 消息json字符串对象
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void deleteLogisticsAddress(String jsonMessage) {
        LogisticsAddressDeleteQueueVO queueVO = ObjectSerializableUtil.deserialize(jsonMessage, LogisticsAddressDeleteQueueVO.class);
        if (queueVO == null) {
            logger.error("删除物流地址消息反序列化错误 =>" + jsonMessage);
            return;
        }

        shipperAddressRepository.deleteByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
        receiverAddressRepository.deleteByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
    }

    /**
     * 启用/停用门店同步更新物流服务地址状态数据
     * @param jsonMessage 消息json字符串对象
     */
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void updateLogisticsAddressStatus(String jsonMessage) {
        LogisticsAddressUpdateQueueVO queueVO = ObjectSerializableUtil.deserialize(jsonMessage, LogisticsAddressUpdateQueueVO.class);
        if (queueVO == null) {
            logger.error("删除物流地址消息反序列化错误 =>" + jsonMessage);
            return;
        }

        ShipperAddress shipperAddress = shipperAddressRepository.findByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
        if (shipperAddress != null) {
            shipperAddress.setStoreStatus(queueVO.getStatus());
            shipperAddressRepository.saveAndFlush(shipperAddress);
        }

        ReceiverAddress receiverAddress = receiverAddressRepository.findByStoreIdAndMemberIdAndRoleId(queueVO.getStoreId(), queueVO.getMemberId(), queueVO.getMemberRoleId());
        if (receiverAddress != null) {
            receiverAddress.setStoreStatus(queueVO.getStatus());
            receiverAddressRepository.saveAndFlush(receiverAddress);
        }
    }
}
